<div class="layuimini-container layuimini-page-anim">
	<div class="layuimini-main">

		<fieldset class="table-search-fieldset">
			<legend>搜索信息</legend>
			<div style="margin: 10px 10px 10px 10px">
				<form class="layui-form layui-form-pane" action="">
					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label ">设备编号</label>
							<div class="layui-input-block">
								<input type="number" name="id" maxLength='12'  value=""  class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label ">设备类型</label>
							<div class="layui-input-block">
								<select id="type" name="type" lay-filter="st"
									item_url="/item_lib/dev_type.json" item-filter="0,1">
									<option value="">全部</option>
								</select>
							</div>
						</div>
						<div class="layui-inline">
								<label class="layui-form-label ">蓝牙MAC</label>
								<div class="layui-input-inline">
									<input type="text" name="bleId" maxLength='16' value=""
										class="layui-input">
								</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">状态</label>
							<div class="layui-input-inline">
								<select id="status" name="status"  lay-filter="st"
									item_url="/item_lib/dev_status.json">
									<option value="">全部</option>
								</select>
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">自编号</label>
							<div class="layui-input-inline">
									<input type="text" name="position" maxLength='16' value=""
										class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<button type="submit" class="layui-btn layui-btn-primary"
								lay-submit lay-filter="data-search-btn">
								<i class="layui-icon"></i>搜 索
							</button>
						</div>
					</div>
				</form>
			</div>
		</fieldset>

		<table class="layui-table" id="currentTableId"
			lay-filter="currentTableFilter">
		</table>
		<script type="text/html" id="switchStatus">
			  <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定" lay-filter="svrStatus" {{ d.status == 1 ? 'checked' : '' }}>
		</script>

		<script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
            	<a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="add">添加</a>
            	<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">报废</a>
             </div>
        </script>
	</div>
</div>


<script>
	
    layui.use(['form', 'table','miniPage','element','common'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
             layarea = layui.layarea,
            miniPage = layui.miniPage;
        
        table.render({
        	elem: '#currentTableId',
        	url: '/device/', 
        	//where:{type:-1},
        	toolbar: '#toolbarDemo', 
        	defaultToolbar: [ 'exports', 'print'], 
			limits: [20, 30, 50, 100], 
			limit: 20,	
			even:true,
			skin: 'row',
			page: true,
              cols: [[
                {type: 'radio', width: 30, title: '选择'},
                {field: 'id', width: 120,  sort: true, title: 'ID'}, 
                {field:'type', minWidth:100, templet:function(d){return layui.common.castValueLable('/item_lib/dev_type.json' ,d.type);}, title: '设备类别'},
                {field:'position', minWidth:200, sort: false, title: '自编号', sort: false},
                {field:'model', minWidth:200,edit: 'text', title: '型号'},
                {field:'batch', minWidth:200,edit: 'text', title: '批次'},
               	{fixed: 'right', field: 'status', width: 100, title: '状态', templet: '#switchStatus', unresize: true, title: '状态'}
              ]]
        });
		
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                 where:  data.field
            	});
            return false;
        });

		//监听单元格编辑
		  table.on('edit(currentTableFilter)', function(obj){
		    var value = obj.value //得到修改后的值
		    ,data = obj.data //得到所在行所有键值
		    ,field = obj.field; //得到字段
		    //layer.msg('[ID: '+ data.id +'] ' + field + ' 字段由'+obj.data[obj.field]+'更改为：'+ value);
		    //layer.msg(JSON.stringify(obj));
		    //console.log(obj)
		    layui.common.ajaxUpdate("/device/update/"+data.id+"/"+field+"/"+value)
		    return false;
		  });
		  
  		//监听锁定操作
  		form.on('switch(svrStatus)', function(obj){
  			var svrSt=-1
  			var svrId=this.value
  			if (obj.elem.checked)
  				svrSt=1
  			layui.common.ajaxUpdate("/device/status/"+svrId+"/"+svrSt)
  		});

        //新增设备
        function add(obj){
            	//var tableData=obj.data;
                var content = miniPage.getHrefContent('/device/add.html');
                var openWH = miniPage.getOpenWidthHeight();
                //console.log(obj)
                window.svrInitData={type:1,status:1};//设置默认服务商类型
                
                var index = layer.open({
                    title: '添加用户',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function () {
                    	if (window.isSubmit){
	                    	table.reload('currentTableId');//刷新数据
							delete window.isSubmit;//清理数据提交标志
                    	}
                    	
    				}
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
        }
         //删除
        function deleteRow(obj,data){
           var index=layer.confirm('真的确定报废设备吗,报废后无法恢复', function (index) {
                 layui.common.ajaxDel("/device/"+data[0].id);
                 layer.close(index);
                 table.reload('currentTableId');//刷新数据
        	});
        }
        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            var data = checkStatus.data;  //获取选中行数据
    		switch(obj.event){
      			case 'add':add(obj)//增加
            			break
      			case 'delete':deleteRow(obj,data)//删除
            			break
            } 
        
        });
    	//初始化选择框    
		layui.common.initSelect();

    });
</script>